package com.yuexun.beilunpatient.ui.registration.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuexun.beilunpatient.R;
import com.yuexun.beilunpatient.base.BaseEntity;
import com.yuexun.beilunpatient.base.BaseKJActivity;
import com.yuexun.beilunpatient.config.AppConfig;
import com.yuexun.beilunpatient.ui.registration.bean.AppointmentLoginBean;
import com.yuexun.beilunpatient.ui.registration.model.impl.RegistrationModel;
import com.yuexun.beilunpatient.ui.registration.presenter.impl.AppointmentLoginPresenter;
import com.yuexun.beilunpatient.ui.registration.ui.fragment.Frag_Registration_Department;
import com.yuexun.beilunpatient.ui.registration.ui.fragment.Frag_Registration_Hospital;
import com.yuexun.beilunpatient.ui.registration.ui.view.IAppointmentLoginView;

import org.kymjs.kjframe.ui.KJFragment;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2018/3/19 0019.
 */

public class Act_Registration extends BaseKJActivity implements IAppointmentLoginView {

    @Bind(R.id.hospital_tv)
    TextView hospitalTv;
    @Bind(R.id.hospital_img)
    ImageView hospitalImg;
    @Bind(R.id.department_tv)
    TextView departmentTv;
    @Bind(R.id.department_img)
    ImageView departmentImg;

    KJFragment fragHospital;
    KJFragment fragDepartment;

    AppointmentLoginPresenter loginPresenter;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_registration);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        loginPresenter = new AppointmentLoginPresenter(new RegistrationModel(), this);
        //type  登录模式（0：会员号登录；1：身份证登录 ），如果type=0，则会员号不能为空，如果type=1，则身份证和姓名不能为空
        //password  会员登录密码
        //idcard 身份证号
        //name 会员名字
        //loginName  会员登录名
        Map<String, String> params = new HashMap<>();
//        params.put("type", "0");//登录模式（0：会员号登录；1：身份证登录 ），如果type=0，则会员号不能为空，如果type=1，则身份证和姓名不能为空
//        params.put("loginName", "java12893");//java12893 java123456
//        params.put("password", "abc123456");
        params.put("sessionUuidPatient", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
        loginPresenter.appointmentLogin(params);

        fragHospital = new Frag_Registration_Hospital();
        fragDepartment = new Frag_Registration_Department();
        changeFragment(R.id.doc_content, fragHospital);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.title_back, R.id.hospital_ll, R.id.department_ll})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.hospital_ll:
                hospitalTv.setTextColor(getResources().getColor(R.color.main_text_02));
                hospitalImg.setBackgroundColor(getResources().getColor(R.color.main_text_02));
                departmentTv.setTextColor(getResources().getColor(R.color.txt_black1));
                departmentImg.setBackgroundColor(getResources().getColor(R.color.transparent));
                changeFragment(R.id.doc_content, fragHospital);
                break;
            case R.id.department_ll:
                hospitalTv.setTextColor(getResources().getColor(R.color.txt_black1));
                hospitalImg.setBackgroundColor(getResources().getColor(R.color.transparent));
                departmentTv.setTextColor(getResources().getColor(R.color.main_text_02));
                departmentImg.setBackgroundColor(getResources().getColor(R.color.main_text_02));
                changeFragment(R.id.doc_content, fragDepartment);
                break;
        }
    }

    @Override
    public void appointmentLogin(BaseEntity<AppointmentLoginBean> baseEntity) {
        if(baseEntity.getStatus().equals("200")){
            AppointmentLoginBean loginBean = baseEntity.getDatas().get(0);
            if(loginBean.getRegisterState() == 0){
                String filename = AppConfig.USER_INFO;
                PreferenceHelper.write(aty, filename, AppConfig.ACCESSTOKEN, loginBean.getAccessToken());
                PreferenceHelper.write(aty, filename, AppConfig.MEMBENUM, loginBean.getMembeNum());
                PreferenceHelper.write(aty, filename, AppConfig.ISREGISTRATIONLOGIN, true);
                PreferenceHelper.write(aty, filename, AppConfig.PHONE, loginBean.getPhone());
            } else if(loginBean.getRegisterState() == 1){
                Intent intent = new Intent(aty, Act_Appointment_Register.class);
                startActivity(intent);
            }
            else if(loginBean.getRegisterState() == 2){
                Bundle bundle = new Bundle();
                bundle.putString("phone", loginBean.getPhone());
                Intent intent = new Intent(aty, Act_Appointment_Auth.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        }
    }
}